import jsonpath

class GlobalVariables:

    __instance = None

    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            cls.__instance = super(GlobalVariables,cls).__new__(cls)
        return cls.__instance

    def __init__(self):
        self.globalVars = {}


    def setVar(self,key,value):
        '''
        设置全局变量
        :param key:
        :param value:
        :return:
        '''
        self.globalVars[key] = value
        print(f'当前全局变量为：{self.globalVars}')

    def getVar(self,key):
        '''
        获取全局变量
        :param key:
        :return:
        '''
        return self.globalVars[key]

    def save_globalVaribales(self,globalVaribale,res):
        '''
        保存全局变量
        :param globalVaribale:  获取的excel内的对应的globalVaribale的表达式
        :param res: 接口返回结果
        :return:
        '''
        for tmp_global in globalVaribale.split(';'):
            temp = tmp_global.strip()
            key = temp.split('=')[0].strip()
            value_express = temp.split('=')[1].strip()
            value = jsonpath.jsonpath(res,value_express)[0]
            self.setVar(key,value)

global_v = GlobalVariables()